<template>
    <a-button type="primary" @click="handleClick" style="margin-left: 10px">
        连读批处理
    </a-button>
</template>

<script setup>
import { message } from "ant-design-vue";
import { ref, watch } from "vue";
import {
    DomEditor,
    SlateEditor,
    SlateElement,
    SlateTransforms,
    SlateNode,
    SlateRange,
    SlateText,
} from "@wangeditor/editor";
import { Range } from "slate";
import ContinuousBacthFn from "./continuous-batch-fn.js";

const editorRef = ref();
const fn = ref();
const onOpen = (editor) => {
    if (!fn.value && editor) {
        // console.log("onOpen");
        editorRef.value = editor;
        fn.value = new ContinuousBacthFn(editor);
    }
};

function handleClick(editor) {
    if (fn.value?.isDisabled()) {
        return;
    }
    var searchString = fn.value?.getValue();
    if (searchString) {
        selectAllOccurrences(searchString);
    }
}

function traverseTree(node, index, path, callback) {
    // 将当前节点的值添加到路径中
    const currentPath = [...path, index];

    // 调用回调函数处理当前节点，包括它的路径
    callback(node, currentPath);
    // 如果存在子节点，递归遍历每个子节点
    if (
        node.type &&
        node.type != "ssml-prosody" &&
        node.children &&
        node.children.length > 0
    ) {
        for (let i = 0; i < node.children.length; i++) {        
            traverseTree(node.children[i], i, currentPath, callback);
        }
    }
}
function findAllPositions(scr, search) {
    // 创建一个正则表达式对象，使用全局搜索标志
    const regex = new RegExp(search, "g");
    let match;
    const positions = [];

    // 使用循环进行全局搜索
    while ((match = regex.exec(scr)) !== null) {
        // 记录匹配的开始和结束索引
        positions.push({
            start: match.index,
            end: match.index + match[0].length - 1,
        });
    }

    return positions;
}

const selectAllOccurrences = (searchString) => {
    var count = 0;
    while(count<1000){
        count++;
        var selectionList = [];
        for (let i = 0; i < editorRef.value.children.length; i++) {
            var item = editorRef.value.children[i];
            traverseTree(item, i, [], (node, path) => {
                console.log(
                    node,
                    path,
                    node.text,
                    searchString,
                    node.text?.includes(searchString)
                );
                if (node && !node.type && node.text) {
                    var text = node.text;
                    if (text.includes(searchString)) {
                        console.log("====add", node.text);
                        const selection = {
                            anchor: {
                                path: path,
                                offset: text.indexOf(searchString),
                            },
                            focus: {
                                path: path,
                                offset:
                                    text.indexOf(searchString) +
                                    searchString.length,
                            },
                        };
                        selectionList.push(selection);
                    }
                }
            });
        }
        if (selectionList.length == 0) {
            return;
        }

        console.log("selectionList", selectionList);
        for (var i in selectionList) {
            var selection = selectionList[i];
            SlateTransforms.select(editorRef.value, selection);
            fn.value?.exec();
        }
    }
};

const props = defineProps({
    editor: {
        type: Object,
        default: null,
    },
});

watch(
    () => props.editor,
    (val) => {
        // console.log("props.editor", props.editor);
        onOpen(props.editor);
    },
    { immediate: true, deep: true }
);

// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen,
});
</script>

<style lang="scss" scoped></style>
